import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'
import { Dialog } from 'vant'
Vue.use(VueRouter)
const router = new VueRouter({
  mode: 'history',
  // history
  // base : "/dist",	//项目打包路径
  routes
})
// 白名单
const whitelist = ['/login', '/', '/home', '/shouye', '/my','/car']

router.beforeEach((to, from, next) => {
  const ttle='当前页面'
  //includes相当于indexof，返回true or false
  const hasWhite = whitelist.includes(to.path)
  document.title=to.meta.title?to.meta.title:ttle
  if (!localStorage.token && !hasWhite) {
    return Dialog.confirm({
      message: '您尚未登录，禁止访问其他页面，是否前往登录！'
    })
      .then(() => {
        next('/login')
      })
      .catch(() => {
        next({
          path: from.path
        })
      })
  } else next()
  // const istoken=localStorage.token?true:false
})
export default router
